﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XeBus.BusinessLogic;
using XeBus.BusinessObject;

namespace XeBus.Controller
{
    public partial class frmLoTrinh_ConDuong : Form
    {

        private LoTrinhBUS ltBUS = new LoTrinhBUS();
        private ConDuongBUS cdBUS = new ConDuongBUS();
        private TramDungBUS tdBUS = new TramDungBUS();
        private ConDuong_LoTrinhBUS clBUS = new ConDuong_LoTrinhBUS();

        private DataTable dt = new DataTable();
        public string ma_tuyen { get; set; }
        private int y;

        public frmLoTrinh_ConDuong()
        {
            InitializeComponent();

            Load_Lo_Trinh();
            Load_Con_Duong();
            Load_ConDuong_LoTrinh();
        }

        private void Load_Lo_Trinh()
        {
            dgvLoTrinh.AutoGenerateColumns = false;
            dgvLoTrinh.DataSource = ltBUS.LoTrinh();
        }

        private void Load_Lo_Trinh(string ma_tuyen)
        {
            dgvLoTrinh.AutoGenerateColumns = false;
            dgvLoTrinh.DataSource = ltBUS.LoTrinh(ma_tuyen);
        }

        private void Load_Con_Duong()
        {
            dgvConDuong.AutoGenerateColumns = false;
            dgvConDuong.DataSource = cdBUS.ConDuong();
        }

        private void Load_ConDuong_LoTrinh()
        {
            dgvCD_LT.AutoGenerateColumns = false;
            dgvCD_LT.DataSource = clBUS.ConDuong_LoTrinh();
        }


        private void btTMLoTrinh_Click(object sender, EventArgs e)
        {
            frmThemSua tlt = new frmThemSua();
            if (ma_tuyen != null)
                tlt.cbbMaTuyen.Text = ma_tuyen;
            tlt.label2.Text = "";
            tlt.ShowDialog();
            if (ma_tuyen != null)
                Load_Lo_Trinh(ma_tuyen);
            else
                Load_Lo_Trinh();
        }

        private void btSuaLoTrinh_Click(object sender, EventArgs e)
        {
            try
            {
                frmThemSua tml = new frmThemSua();
                tml.label1.Text = "Sửa lộ trình:";
                tml.txtTenLoTrinh.Text = dt.Rows[0]["Ten_Lo_Trinh"].ToString();
                tml.txtHuongDi.Text = dt.Rows[0]["Huong_Di"].ToString();
                tml.ma_lo_trinh = dt.Rows[0]["Ma_Lo_Trinh"].ToString();
                tml.label2.Text = "mã lộ trình " + dt.Rows[0]["Ma_Lo_Trinh"].ToString();
                tml.cbbMaTuyen.Text = dt.Rows[0]["Ma_Tuyen"].ToString();
                tml.ShowDialog();
                if (ma_tuyen != null)
                    Load_Lo_Trinh(ma_tuyen);
                else
                    Load_Lo_Trinh();
            }
            catch (Exception) { }
        }

        private void btXoaLoTrinh_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có chắc chắn muốn xóa không ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    y = dgvLoTrinh.CurrentCell.RowIndex;
                    string ma_lo_trinh = dgvLoTrinh.Rows[y].Cells[0].Value.ToString();
                    ltBUS.XOA(ma_lo_trinh);//ltObj.Ma_Lo_Trinh);
                    MessageBox.Show("Xóa dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    MessageBox.Show("Xóa dữ liệu thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                if (ma_tuyen != null)
                    Load_Lo_Trinh(ma_tuyen);
                else
                    Load_Lo_Trinh();
            }
        }

        private void btDongLoTrinh_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Hien_Thi_Lo_Trinh(int y)
        {
            string id = dgvLoTrinh.Rows[y].Cells[0].Value.ToString();
            dt = ltBUS.LoTrinhTheoID(id);

            lblMaLoTrinh.Text = dt.Rows[0]["Ma_Lo_Trinh"].ToString();
            lblTenLoTrinh.Text = dt.Rows[0]["Ten_Lo_Trinh"].ToString();
            lblHuongDi.Text = dt.Rows[0]["Huong_Di"].ToString();
            lblMaTuyen.Text = dt.Rows[0]["Ma_Tuyen"].ToString();
        }

        private void dgvLoTrinh_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            y = e.RowIndex;
            Hien_Thi_Lo_Trinh(y);
        }

        private void btTMConDuong_Click(object sender, EventArgs e)
        {
            frmThemSua ts = new frmThemSua();
            ts.label1.Text = "Thêm con đường:";
            ts.label2.Text = "";
            ts.lblTenLoTrinh.Text = "Tên con đường:";
            ts.lblHuongDi.Text = "Quận/Huyện:";
            ts.lblMaTuyen.Visible = false;
            ts.cbbMaTuyen.Visible = false;

            ts.ShowDialog();
            Load_Con_Duong();
        }

        private void btSuaConDuong_Click(object sender, EventArgs e)
        {
            frmThemSua ts = new frmThemSua();
            ts.label1.Text = "Sửa con đường:";
            ts.lblTenLoTrinh.Text = "Tên con đường:";
            ts.lblHuongDi.Text = "Quận/Huyện:";
            ts.txtTenLoTrinh.Text = dt.Rows[0]["Ten_Duong"].ToString();
            ts.txtHuongDi.Text = dt.Rows[0]["Quan_Huyen"].ToString();
            ts.label2.Text = "mã con đường " + dt.Rows[0]["Ma_Con_Duong"].ToString();
            ts.ma_con_duong = dt.Rows[0]["Ma_Con_Duong"].ToString();
            ts.lblMaTuyen.Visible = false;
            ts.cbbMaTuyen.Visible = false;
            ts.ShowDialog();
            Load_Con_Duong();
        }

        private void btXoaConDuong_Click(object sender, EventArgs e)
        {

            DialogResult dr = MessageBox.Show("Bạn có chắc chắn muốn xóa không ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    y = dgvConDuong.CurrentCell.RowIndex;
                    string ma_con_duong = dgvConDuong.Rows[y].Cells[0].Value.ToString();
                    cdBUS.XOA(ma_con_duong);//cd.Ma_Con_Duong);
                    MessageBox.Show("Xóa dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    MessageBox.Show("Xóa dữ liệu thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                Load_Con_Duong();
            }
        }

        private void btSuaTramDung_Click(object sender, EventArgs e)
        {
            try
            {
                frmTramDung td = new frmTramDung();
                td.ma_con_duong = dt.Rows[0]["Ma_Con_Duong"].ToString();
                td.lblMaConDuong.Text = dt.Rows[0]["Ma_Con_Duong"].ToString();
                td.dgvTramDung.AutoGenerateColumns = false;
                td.dgvTramDung.DataSource = tdBUS.TramDung(td.ma_con_duong);
                td.ShowDialog();
            }
            catch (Exception) { }
        }

        private void btDongConDuong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Hien_Thi_Con_Duong(int y)
        {
            string id = dgvConDuong.Rows[y].Cells[0].Value.ToString();
            dt = cdBUS.ConDuongTheoID(id);

            lblMaConDuong.Text = dt.Rows[0]["Ma_Con_Duong"].ToString();
            lblQuan_Huyen.Text = dt.Rows[0]["Quan_Huyen"].ToString();
            lblTenDuong.Text = dt.Rows[0]["Ten_Duong"].ToString();
        }

        private void dgvConDuong_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            y = e.RowIndex;
            Hien_Thi_Con_Duong(y);
        }

        private void btTM_LT_CD_Click(object sender, EventArgs e)
        {
            frmThemSuaCD_LT cl = new frmThemSuaCD_LT();
            cl.ShowDialog();
            Load_ConDuong_LoTrinh();
        }

        private void btSuaLT_CD_Click(object sender, EventArgs e)
        {
            frmThemSuaCD_LT cl = new frmThemSuaCD_LT();
            cl.label3.Text = "Sửa: ID = " + dt.Rows[0]["ID"].ToString();
            cl.ID = dt.Rows[0]["ID"].ToString();
            cl.cbbMaLoTrinh.Text = dt.Rows[0]["Ma_Lo_Trinh"].ToString();
            cl.cbbMaConDuong.Text = dt.Rows[0]["Ma_Con_Duong"].ToString();
            cl.txtSoThuTu.Text = dt.Rows[0]["So_Thu_Tu"].ToString();
            cl.ShowDialog();
            Load_ConDuong_LoTrinh();
        }

        private void btXoaLT_CD_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có chắc chắn muốn xóa không ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    y = dgvCD_LT.CurrentCell.RowIndex;
                    string ID = dgvCD_LT.Rows[y].Cells[0].Value.ToString();
                    clBUS.XOA(ID);
                    MessageBox.Show("Xóa dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    MessageBox.Show("Xóa dữ liệu thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                Load_ConDuong_LoTrinh();
            }
        }

        private void btDongLT_CD_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Hien_Thi_LT_CD(int y)
        {
            string id = dgvCD_LT.Rows[y].Cells[0].Value.ToString();
            dt = clBUS.ConDuong_LoTrinhTheoID(id);

            lblID.Text = dt.Rows[0]["ID"].ToString();
            lblMa_Lo_Trinh.Text = dt.Rows[0]["Ma_Lo_Trinh"].ToString();
            lblMa_Con_Duong.Text = dt.Rows[0]["Ma_Con_Duong"].ToString();
            lblSoThuTu.Text = dt.Rows[0]["So_Thu_Tu"].ToString();
            lblTen_Con_Duong.Text = dt.Rows[0]["Ten_Duong"].ToString();
            lblTen_Lo_Trinh.Text = dt.Rows[0]["Ten_Lo_Trinh"].ToString();
        }

        private void dgvCD_LT_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            y = e.RowIndex;
            Hien_Thi_LT_CD(y);
        }

    }
}
